﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YTSK.Model;
using System.Xml;
using System.Windows.Forms;

namespace YTSK.cSK.BW
{
    public class cMakeXML_DZP
    {
        public static string MakeXML(string savefile, Model.SK_SalebillVouch  sk, List<Model.SK_SalebillVouchs_View> l_sks)
        {
            cSKSet:
            var _FHR = cSK.cSKSet.loadsave(SkConfig.复核人);
            var _SKR = cSK.cSKSet.loadsave(SkConfig.收款人);
            var _fpdz = cSK.cSKSet.loadsave(SkConfig.默认发票地址电话);
            var  _yh = cSK.cSKSet.loadsave(SkConfig.默认银行及账号);
            var _version = cSK.cSKSet.loadsave(SkConfig.税控分类版本号);
            if (_version.Length == 0)
            {
                MessageBox.Show("请先设置税控设备参数");
                var f = new FrmSKSet();
                f.ShowDialog();
                goto cSKSet;
            }

            var xml = new XmlDocument();
            var declare = xml.CreateXmlDeclaration("1.0", "GBK", null);
            xml.AppendChild(declare);
            #region 表头定义
            var kp = xml.CreateElement("business");
            var attr = xml.CreateAttribute("id");
            attr.Value = "FPKJ";
            kp.Attributes.Append(attr);
            attr = xml.CreateAttribute("comment");
            attr.Value = "发票开具";
            kp.Attributes.Append(attr);

            var REQUEST_COMMON_FPKJ = xml.CreateElement("REQUEST_COMMON_FPKJ");
            attr = xml.CreateAttribute("class");
            attr.Value = "REQUEST_COMMON_FPKJ";
            var topmain = xml.CreateElement("COMMON_FPKJ_FPT");
            attr = xml.CreateAttribute("class");
            attr.Value = "COMMON_FPKJ_FPT";
            topmain.Attributes.Append(attr);
            #endregion

            #region 表头栏目
            var top = xml.CreateElement("FPQQLSH");
            top.InnerText = DateTime.Now.ToString("yyMMddhhmmss");
            topmain.AppendChild(top);
            top = xml.CreateElement("KPLX");
            top.InnerText = "0";
            topmain.AppendChild(top);
            top = xml.CreateElement("BMB_BBH");
            top.InnerText = "17.0";
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_NSRSBH");
            top.InnerText = "";
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_MC");
            top.InnerText = "";
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_DZDH");
            top.InnerText = _fpdz;
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_YHZH");
            top.InnerText = _yh;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_NSRSBH");
            top.InnerText = sk.TaxNo;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_MC");
            top.InnerText = sk.cCusName;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_DZDH");
            top.InnerText = string.Format("{0} {1}", sk.Address, sk.Phone);
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_YHZH");
            top.InnerText = string.Format("{0} {1}", sk.Bank, sk.BankAccount);
            topmain.AppendChild(top);
            top = xml.CreateElement("KPR");
            top.InnerText = "开票人";
            topmain.AppendChild(top);
            top = xml.CreateElement("SKR");
            top.InnerText = _SKR;
            topmain.AppendChild(top);
            top = xml.CreateElement("FHR");
            top.InnerText = _FHR;
            topmain.AppendChild(top);
            top = xml.CreateElement("YFP_DM");
            top.InnerText = "";
            topmain.AppendChild(top);
            top = xml.CreateElement("YFP_HM");
            top.InnerText = "";
            topmain.AppendChild(top);
            #endregion

            #region 明细开票
            var main = xml.CreateElement("COMMON_FPKJ_XMXXS");
            attr = xml.CreateAttribute("class");
            attr.Value = "COMMON_FPKJ_XMXX";
            main.Attributes.Append(attr);
            attr = xml.CreateAttribute("size");
            attr.Value = "1";
            main.Attributes.Append(attr);
            decimal sumjshj = 0;
            decimal sumje = 0;
            decimal sumse = 0;

            int irowcount = 1;
            for (int i = 0; i < l_sks.Count; i++)
            {
                var t= l_sks[i];
                if (t.cInvName.ObjToString().Length > 0)
                {
                    #region 非折扣行
                    if (t.iDiscountSum == 0)
                    {
                        var d = xml.CreateElement("COMMON_FPKJ_XMXX");
                        var cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        { throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType)); }
                        var detail = xml.CreateElement("SPBM");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);

                        detail = xml.CreateElement("ZXBM");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("YHZCBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("ZZSTSGL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("LSLBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("FPHXZ");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMMC");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("GGXH");
                        detail.InnerText = t.cInvStd;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("DW");
                        detail.InnerText = t.cDW;
                        d.AppendChild(detail);
                        var jehs = t.iSumSale.ObjToDecimal();
                        var taxrate = t.iTaxRateSale.ObjToDecimal();
                        var jeori = jehs / (1 + taxrate / 100);
                        decimal jeinput = Math.Round(jeori, 15, MidpointRounding.AwayFromZero);
                        var se = jehs - jeinput;
                        var sl = t.iQuantity;
                        var dj = jeinput / sl;
                        var itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        sumjshj += jehs;
                        sumje += jeinput;
                        sumse += se;
                        detail = xml.CreateElement("XMSL");
                        detail.InnerText = sl.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMDJ");
                        detail.InnerText = dj.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMHJ");
                        detail.InnerText = jehs.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMJE");
                        detail.InnerText = jeinput.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SL");
                        detail.InnerText = itaxrate.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SE");
                        detail.InnerText = se.ObjToString();
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;
                    }
                    #endregion

                    #region 折扣行
                    else
                    {
                        #region 原价
                        var d = xml.CreateElement("COMMON_FPKJ_XMXX");
                        var cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        { throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType)); }
                        var detail = xml.CreateElement("SPBM");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);

                        detail = xml.CreateElement("ZXBM");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("YHZCBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("ZZSTSGL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("LSLBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("FPHXZ");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMMC");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("GGXH");
                        detail.InnerText = t.cInvStd;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("DW");
                        detail.InnerText = t.cDW;
                        d.AppendChild(detail);
                        var jehs = t.iQuoteSum.ObjToDecimal();
                        var taxrate = t.iTaxRateSale.ObjToDecimal();
                        var jeori = jehs / (1 + taxrate / 100);
                        decimal jeinput = Math.Round(jeori, 15, MidpointRounding.AwayFromZero);
                        var se = jehs - jeinput;
                        var sl = t.iQuantity;
                        var dj = jeinput / sl;
                        var itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        sumjshj += jehs;
                        sumje += jeinput;
                        sumse += se;
                        detail = xml.CreateElement("XMSL");
                        detail.InnerText = sl.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMDJ");
                        detail.InnerText = dj.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMHJ");
                        detail.InnerText = jehs.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMJE");
                        detail.InnerText = jeinput.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SL");
                        detail.InnerText = itaxrate.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SE");
                        detail.InnerText = se.ObjToString();
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;
                        #endregion

                        #region 折扣
                        d = xml.CreateElement("COMMON_FPKJ_XMXX");
                        cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        { throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType)); }
                        detail = xml.CreateElement("SPBM");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);

                        detail = xml.CreateElement("ZXBM");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("YHZCBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("ZZSTSGL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("LSLBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("FPHXZ");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMMC");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("GGXH");
                        detail.InnerText = t.cInvStd;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("DW");
                        detail.InnerText = t.cDW;
                        d.AppendChild(detail);
                        jehs = -t.iDiscountSum.ObjToDecimal();
                        taxrate = t.iTaxRateSale.ObjToDecimal();
                        jeori = jehs / (1 + taxrate / 100);
                        jeinput = Math.Round(jeori, 15, MidpointRounding.AwayFromZero);
                        se = jehs - jeinput;
                        sl = t.iQuantity;
                        dj = jeinput / sl;
                        itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        sumjshj += jehs;
                        sumje += jeinput;
                        sumse += se;
                        detail = xml.CreateElement("XMSL");
                        //detail.InnerText = sl.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMDJ");
                        //detail.InnerText = dj.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMHJ");
                        detail.InnerText = jehs.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMJE");
                        detail.InnerText = jeinput.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SL");
                        detail.InnerText = itaxrate.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SE");
                        detail.InnerText = se.ObjToString();
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;
                        #endregion
                    }

                    #endregion
                }
            }
            #endregion

            #region 表头项目增加
            top = xml.CreateElement("JSHJ");
            top.InnerText = sumjshj.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("HJJE");
            top.InnerText = sumje.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("HJSE");
            top.InnerText = sumse.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("BZ");
            top.InnerText = sk.cDf10;
            topmain.AppendChild(top);
            #endregion

            #region 完成XML文件
            REQUEST_COMMON_FPKJ.AppendChild(topmain);
            REQUEST_COMMON_FPKJ.AppendChild(main);
            kp.AppendChild(REQUEST_COMMON_FPKJ);
            xml.AppendChild(kp);
            #endregion

            #region 保存文件
            if (System.IO.File.Exists(savefile))
            { System.IO.File.Delete(savefile); }
            xml.Save(savefile);
            return savefile;
            #endregion

        }



    }
}
